//
//  SGSDelegatesManager.m
//  AgileNYC
//
//  Created by German on 03.08.14.
//  Copyright (c) 2014 Softlegion. All rights reserved.
//

#import "SGSDelegatesManager.h"
#import "SGSUtils.h"

static NSString * EventsControllerName = @"SGSEventsViewController";
static NSString * LoginControllerName = @"SGSLoginViewController";
static NSString * ProfileROControllerName = @"SGSProfileROViewController";
static NSString * ProfileTableROControllerName = @"SGSProfileROTableViewController";
static NSString * ProfileControllerName = @"SGSProfileViewController";
static NSString * ProfileTableControllerName = @"SGSProfileTableViewController";
static NSString * ContactsControllerName = @"SGSContactsViewController";
static NSString * EventDescriptionControllerName = @"SGSEventDescriptionViewController";
static NSString * HelpControllerName = @"SGSHelpViewController";
static NSString * SignInWithEmailControllerName = @"SGSSignInWithEmailViewController";

@interface SGSDelegatesManager ()

@property (strong, nonatomic) NSString * currentStoryboardName;
@property (strong, nonatomic) UIStoryboard * mainStoryboard;

@end



@implementation SGSDelegatesManager

@synthesize currentEventsVC = _currentEventsVC;
@synthesize currentProfileVC = _currentProfileVC;
@synthesize currentProfileTableVC = _currentProfileTableVC;
@synthesize currentProfileROVC = _currentProfileROVC;
@synthesize currentProfileROTableVC = _currentProfileROTableVC;
@synthesize currentLoginVC = _currentLoginVC;
@synthesize currentContactsVC = _currentContactsVC;
@synthesize currentEventDescriptionVC = _currentEventDescriptionVC;
@synthesize currentHelpVC = _currentHelpVC;
@synthesize currentSigninWithEmailVC = _currentSigninWithEmailVC;

@synthesize controllerToBeOpened = _controllerToBeOpened;

+(SGSDelegatesManager*) sharedDelegatesManager {
    static SGSDelegatesManager * manager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[SGSDelegatesManager alloc] init];
    });
    
    return manager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.currentStoryboardName = getCurrentStoryBoardName();
        self.mainStoryboard = [UIStoryboard storyboardWithName:self.currentStoryboardName bundle:nil];
    }
    return self;
}


#pragma mark - Getters and Setters

-(UIViewController *)controllerToBeOpened {
    return _controllerToBeOpened;
}

-(void) setControllerToBeOpened:(UIViewController *)controllerToBeOpened {
    _controllerToBeOpened = controllerToBeOpened;
}

-(SGSEventsViewController*) currentEventVC {
    
    if (!_currentEventsVC) {
        _currentEventsVC = [self.mainStoryboard instantiateViewControllerWithIdentifier:EventsControllerName];
    }
    
    return _currentEventsVC;
}

-(void) setCurrentEventsVC:(SGSEventsViewController *)currentEventsVC {
    _currentEventsVC = currentEventsVC;
}

-(SGSProfileROViewController*) currentProfileROVC {
    
    if (!_currentProfileROVC) {
        _currentProfileROVC = [self.mainStoryboard instantiateViewControllerWithIdentifier:ProfileROControllerName];
    }
    
    return _currentProfileROVC;
}

-(void) setCurrentProfileROVC:(SGSProfileROViewController *)currentProfileROVC {
    _currentProfileROVC = currentProfileROVC;
}

-(SGSProfileViewController*) currentProfileVC {

    if (!_currentProfileVC) {
        _currentProfileVC = [self.mainStoryboard instantiateViewControllerWithIdentifier:ProfileControllerName];
    }
    
    return _currentProfileVC;
}

-(void) setCurrentProfileVC:(SGSProfileViewController *)currentProfileVC {
    _currentProfileVC = currentProfileVC;
}

-(SGSProfileTableViewController *)currentProfileTableVC {

    if (!_currentProfileTableVC) {
        _currentProfileTableVC = [self.mainStoryboard instantiateViewControllerWithIdentifier:ProfileTableControllerName];
    }
    
    return _currentProfileTableVC;
}

-(void) setCurrentProfileTableVC:(SGSProfileTableViewController *)currentProfileTableVC {
    _currentProfileTableVC = currentProfileTableVC;
}

-(SGSProfileROTableViewController *)currentProfileROTableVC {
    
    if (!_currentProfileROTableVC) {
        _currentProfileROTableVC = [self.mainStoryboard instantiateViewControllerWithIdentifier:ProfileTableROControllerName];
    }
    
    return _currentProfileROTableVC;
}

-(void) setCurrentProfileROTableVC:(SGSProfileROTableViewController *)currentProfileROTableVC {
    _currentProfileROTableVC = currentProfileROTableVC;
}

-(SGSLoginViewController *)currentLoginVC {

    if (!_currentLoginVC) {
        _currentLoginVC = [self.mainStoryboard instantiateViewControllerWithIdentifier:LoginControllerName];
    }
    
    return _currentLoginVC;
    
}

-(void) setCurrentLoginVC:(SGSLoginViewController *)currentLoginVC {
    _currentLoginVC = currentLoginVC;
}

-(SGSContactsViewController *)currentContactsVC {

    if (!_currentContactsVC) {
        _currentContactsVC = [self.mainStoryboard instantiateViewControllerWithIdentifier:ContactsControllerName];
    }
    
    return _currentContactsVC;
    
}

-(void)setCurrentContactsVC:(SGSContactsViewController *)currentContactsVC {
    _currentContactsVC = currentContactsVC;
}

-(SGSEventDescriptionViewController *)currentEventDescriptionVC {

    if (!_currentEventDescriptionVC) {
        _currentEventDescriptionVC = [self.mainStoryboard instantiateViewControllerWithIdentifier:EventDescriptionControllerName];
    }
    
    return _currentEventDescriptionVC;
    
}

-(void)setCurrentEventDescriptionVC:(SGSEventDescriptionViewController *)currentEventDescriptionVC {
    _currentEventDescriptionVC = currentEventDescriptionVC;
}

-(SGSHelpViewController *)currentHelpVC {

    if (!_currentHelpVC) {
        _currentHelpVC = [self.mainStoryboard instantiateViewControllerWithIdentifier:HelpControllerName];
    }
    
    return _currentHelpVC;
    
}

-(void) setCurrentHelpVC:(SGSHelpViewController *)currentHelpVC {
    _currentHelpVC = currentHelpVC;
}

-(SGSSignInWithEmailViewController *)currentSigninWithEmailVC {

    if (!_currentSigninWithEmailVC) {
        _currentSigninWithEmailVC = [self.mainStoryboard instantiateViewControllerWithIdentifier:SignInWithEmailControllerName];
    }
    
    return _currentSigninWithEmailVC;
    
}

-(void) setCurrentSigninWithEmailVC:(SGSSignInWithEmailViewController *)currentSigninWithEmailVC {
    _currentSigninWithEmailVC = currentSigninWithEmailVC;
}

-(UIViewController*) getControllerByName:(NSString*) controllerName {
    if ([controllerName rangeOfString:EventsControllerName
                              options:NSCaseInsensitiveSearch].location!=NSNotFound) {
    
        return self.currentEventsVC;
    }

    if ([controllerName rangeOfString:ProfileROControllerName
                              options:NSCaseInsensitiveSearch].location!=NSNotFound) {
        
        return self.currentProfileROVC;
    }

    if ([controllerName rangeOfString:ProfileTableROControllerName
                              options:NSCaseInsensitiveSearch].location!=NSNotFound) {
        
        return self.currentProfileROTableVC;
    }

    
    if ([controllerName rangeOfString:ProfileControllerName
                              options:NSCaseInsensitiveSearch].location!=NSNotFound) {
        
        return self.currentProfileVC;
    }

    if ([controllerName rangeOfString:ProfileTableControllerName
                              options:NSCaseInsensitiveSearch].location!=NSNotFound) {
        
        return self.currentProfileTableVC;
    }

    if ([controllerName rangeOfString:LoginControllerName
                              options:NSCaseInsensitiveSearch].location!=NSNotFound) {
        
        return self.currentLoginVC;
    }

    if ([controllerName rangeOfString:ContactsControllerName
                              options:NSCaseInsensitiveSearch].location!=NSNotFound) {
        
        return self.currentContactsVC;
    }

    if ([controllerName rangeOfString:EventDescriptionControllerName
                              options:NSCaseInsensitiveSearch].location!=NSNotFound) {
        
        return self.currentEventDescriptionVC;
    }
    
    if ([controllerName rangeOfString:HelpControllerName
                              options:NSCaseInsensitiveSearch].location!=NSNotFound) {
        
        return self.currentHelpVC;
    }

    if ([controllerName rangeOfString:SignInWithEmailControllerName
                              options:NSCaseInsensitiveSearch].location!=NSNotFound) {
        
        return self.currentSigninWithEmailVC;
    }

    return nil;
}

@end
